package com.stormragetech.teamwork.module.project.spring.service.impl;

import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.entity.Struc;
import com.stormragetech.teamwork.module.project.entity.ProjectStruc;
import com.stormragetech.teamwork.module.project.spring.dao.ProjectStrucMongo;
import com.stormragetech.teamwork.module.project.spring.service.ProjectStrucService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Service
public class ProjectStrucServiceImpl implements ProjectStrucService {

    private final ProjectStrucMongo projectModelMongo;

    @Autowired
    public ProjectStrucServiceImpl(ProjectStrucMongo projectModelMongo) {
        Assert.notNull(projectModelMongo, "ProjectStrucMongo cannot be null !!!");
        this.projectModelMongo = projectModelMongo;
    }

    @Override
    public void create(final String[] name, final String[] type, final String version) throws StormragetechException {

        List<Struc> strucs = new ArrayList<>();
        for (int i = 0; i < name.length; i++) {
            if (null != name[i] && name[i].length() > 0 && null != type[i] && type[i].length() > 0) {
                strucs.add(new Struc("key" + i, name[i], type[i]));
            }
        }

        projectModelMongo.create(new ProjectStruc(strucs, new Date(), version));

    }

    @Override
    public ProjectStruc findLastStruc() throws StormragetechException {

        return projectModelMongo.findLastStruc();
    }

}
